package autokeyboard.handler;

import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import autokeyboard.data.KeyStrokeData;
import autokeyboard.data.TableModel;
import autokeyboard.gui.KeyStrokeDialog;
import autokeyboard.gui.TablePanel;

public class TablePanelEvent
{
	private ShowDialogEvent	showDlg			= new ShowDialogEvent();
	private KeyStrokeDialog	keyStrokeDialog;
	private KeyStrokeData	keyStrokeData	= new KeyStrokeData();

	private TableModel		model			= new TableModel();
	private JTable			table;

	private int				indexEdit;

	private final int		MOVE_UP			= 0;
	private final int		MOVE_DOWN		= 1;
	private final int		DELETE			= 2;

	private int				choice;

	private void setTable(JTable table)
	{
		this.table = table;
	}

	private void setIndexEdit(int index)
	{
		this.indexEdit = index;
	}

	/**
	 * This method will show keyStrokeDialog and set your default key to
	 * KeyStrokeDialog with your input list Object
	 * 
	 * @param frame
	 * @param list
	 */
	public void showKeyStrokeDialog(JFrame frame, JTable table, Object[] list, int index, ArrayList<Object[]> listObject)
	{
		setTable(table);
		setIndexEdit(index);

		keyStrokeData.setKeyStrokeData(list);
		this.keyStrokeDialog = showDlg.showKeyStrokeDialog(frame, list);
		if (!keyStrokeDialog.isShowing())
		{
			listObject.set(this.indexEdit, keyStrokeDialog.getNewObjectList());
		}
		Object[][] listNew = new Object[listObject.size()][];

		for (int i = 0; i < listObject.size(); i++)
		{
			listNew[i] = listObject.get(i);
		}

		this.table.setModel(model.setNewModel(listNew));
		columnAlignment(JLabel.LEFT);
	}

	/**
	 * This method will align your column in your table with your input
	 * alignment
	 * 
	 * @param alignment
	 */
	private void columnAlignment(int alignment)
	{
		DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
		cellRenderer.setHorizontalAlignment(alignment);
		table.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
		table.getColumnModel().getColumn(1).setCellRenderer(cellRenderer);
		table.getColumnModel().getColumn(2).setCellRenderer(cellRenderer);
		table.getColumnModel().getColumn(3).setCellRenderer(cellRenderer);
	}

	/**
	 * This method will move up your selected row return an edited list object
	 * with your input listObject
	 * 
	 * @param listObject
	 * @param index
	 * @return edited list Object
	 */
	public ArrayList<Object[]> moveUpOneLine(ArrayList<Object[]> listObject, int index, JButton btDown, JButton btUp)
	{
		choice = MOVE_UP;
		ArrayList<Object[]> listTemp = listObject;
		if (index <= 1)
		{
			btUp.setEnabled(false);
		}
		if (index != 0)
		{
			Object[] temp = listTemp.get(index);
			listTemp.set(index, listTemp.get(index - 1));
			listTemp.set(index - 1, temp);

			String indexTemp = listTemp.get(index)[0].toString();
			listTemp.get(index)[0] = listTemp.get(index - 1)[0];
			listTemp.get(index - 1)[0] = indexTemp;
			btDown.setEnabled(true);
		}
		return listTemp;
	}

	/**
	 * This method will move down your selected row and then return an edited
	 * list object with your input listObject
	 * 
	 * @param listObject
	 * @param index
	 * @return edited list Object
	 */
	public ArrayList<Object[]> moveDownOneLine(ArrayList<Object[]> listObject, int index, JButton btDown, JButton btUp)
	{
		choice = MOVE_DOWN;

		ArrayList<Object[]> listTemp = listObject;

		if (index >= listTemp.size() - 2)
		{
			btDown.setEnabled(false);
		}
		if (index != listTemp.size() - 1)
		{
			Object[] temp = listTemp.get(index);
			listTemp.set(index, listTemp.get(index + 1));
			listTemp.set(index + 1, temp);

			String indexTemp = listTemp.get(index)[0].toString();
			listTemp.get(index)[0] = listTemp.get(index + 1)[0];
			listTemp.get(index + 1)[0] = indexTemp;
			btUp.setEnabled(true);
		}

		return listTemp;
	}

	/**
	 * This method will delete your selected row and return an edited listObject
	 * 
	 * @param listObject
	 * @param index
	 * @return
	 */
	public ArrayList<Object[]> deleteOneLine(ArrayList<Object[]> listObject, int index, JButton btDelete, JButton btUp, JButton btDown)
	{
		choice = DELETE;
		ArrayList<Object[]> listTemp = listObject;

		listTemp.remove(index);
		TablePanel.isChanged = true;
		
		if (listTemp.size() == 0)
		{
			btDelete.setEnabled(false);
			btUp.setEnabled(false);
			btDown.setEnabled(false);
			TablePanel.isChanged = false;
		}
		for (int i = 0; i < listTemp.size(); i++)
		{
			listTemp.get(i)[0] = i + 1;
		}

		return listTemp;
	}

	/**
	 * This method will update your input table with your listObject model
	 * <p>
	 * And it also align your table to the left alignment
	 * 
	 * @param table
	 * @param listObject
	 */
	public void updateTable(JTable table, ArrayList<Object[]> listObject, int index)
	{
		setTable(table);
		Object[][] list = new Object[listObject.size()][];
		
		TablePanel.rowCount = list.length;

		for (int i = 0; i < list.length; i++)
		{
			list[i] = listObject.get(i);
		}
		table.setModel(model.setNewModel(list));
		columnAlignment(JLabel.LEFT);
		switch (choice)
		{
			case MOVE_UP:
			{
				if (index > 0) table.setRowSelectionInterval(index, index - 1);
				if (index <= 0) table.setRowSelectionInterval(index, index);
			}
			break;
			case MOVE_DOWN:
			{
				if (index < list.length - 1) table.setRowSelectionInterval(index, index + 1);
				if (index == list.length - 1) table.setRowSelectionInterval(index - 1, index);
			}
			break;
			case DELETE:
			{
				if (index < list.length - 1)
				{
					if(index != 0)
					{
						table.setRowSelectionInterval(index - 1, index);
					}
					else if(index == 0)
					{
						table.setRowSelectionInterval(index, index);
					}
					
				}
				else if (index == list.length - 1 || index == list.length)
				{
					if (list.length >= 2)
					{
						table.setRowSelectionInterval(list.length - 2, list.length - 1);
					}
					else if (list.length < 2 && list.length >= 1)
					{
						table.setRowSelectionInterval(0, 0);
					}
				}
			}
			break;
		}
	}
}
